package com.one.blocks.rbac.controller.convert;

import com.one.blocks.rbac.controller.convert.worker.BaseDomainExcelConvert;
import com.one.blocks.rbac.controller.convert.worker.TimeConvert;
import com.one.blocks.rbac.controller.query.SysUserQuery;
import com.one.blocks.rbac.controller.vo.SysUserGrantRoleVo;
import com.one.blocks.rbac.controller.vo.SysUserGroupVo;
import com.one.blocks.rbac.controller.vo.SysUserRoleVo;
import com.one.blocks.rbac.controller.vo.SysUserVo;
import com.one.blocks.rbac.domain.SysUser;
import com.one.blocks.rbac.domain.SysUserAccount;
import com.one.blocks.rbac.domain.SysUserRole;
import com.one.blocks.security.util.SubjectHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_CREATE_BY;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_CREATE_TIME;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_ID;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_UPDATE_BY;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_UPDATE_TIME;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-05-01 10:06:14
 */
@Mapper(uses = {TimeConvert.class}, imports = {SubjectHelper.class})
public interface SysUserConvert extends BaseDomainExcelConvert<SysUser, SysUserVo, SysUserQuery, SysUserVo> {

    SysUserConvert INSTANCE = Mappers.getMapper(SysUserConvert.class);

    /**
     * 添加参数转换
     *
     * @param sysUserVo 前端参数对象
     * @return 数据对象
     */
    @Named(value = "voToEntityIgnoreId")
    @Mapping(target = CONVERT_ID, ignore = true)
    @Mapping(target = CONVERT_CREATE_BY, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = CONVERT_CREATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @Mapping(target = CONVERT_UPDATE_BY, ignore = true)
    @Mapping(target = CONVERT_UPDATE_TIME, ignore = true)
    @Mapping(target = "gender", defaultValue = "0")
    @Override
    SysUser voToEntityIgnoreId(SysUserVo sysUserVo);

    /**
     * 数据对象转换
     *
     * @param sysUserGroupVo 前端参数对象
     * @return 数据对象
     */
    @Named(value = "groupToEntityIgnoreId")
    @InheritConfiguration(name = "voToEntityIgnoreId")
    SysUser groupToEntityIgnoreId(SysUserGroupVo sysUserGroupVo);

    default SysUserRole toSysUserRole(SysUserRoleVo sysUserRoleVo) {
        SysUserRole result = new SysUserRole();
        result.setUserId(sysUserRoleVo.getUserId());
        result.setRoleId(sysUserRoleVo.getRoleId());
        result.setCreateBy(SubjectHelper.getSubjectId());
        result.setCreateTime(SubjectHelper.getAccessTime());
        return result;
    }

    default List<SysUserRole> grantToSysUserRole(SysUserGrantRoleVo sysUserGrantRoleVo) {
        if (CollectionUtils.isEmpty(sysUserGrantRoleVo.getRoleIds())) {
            return null;
        }
        List<SysUserRole> result = new ArrayList<>(sysUserGrantRoleVo.getRoleIds().size());
        for (Integer roleId : sysUserGrantRoleVo.getRoleIds()) {
            SysUserRole item = new SysUserRole();
            item.setUserId(sysUserGrantRoleVo.getUserId());
            item.setRoleId(roleId);
            item.setCreateBy(SubjectHelper.getSubjectId());
            item.setCreateTime(SubjectHelper.getAccessTime());
        }
        return result;
    }

    default List<SysUserRole> groupToSysUserRole(SysUserGroupVo sysUserGroupVo) {
        if (CollectionUtils.isEmpty(sysUserGroupVo.getRoleIds())) {
            return null;
        }
        List<SysUserRole> result = new ArrayList<>(sysUserGroupVo.getRoleIds().size());
        for (Integer roleId : sysUserGroupVo.getRoleIds()) {
            SysUserRole item = new SysUserRole();
            item.setUserId(sysUserGroupVo.getId());
            item.setRoleId(roleId);
            item.setCreateBy(SubjectHelper.getSubjectId());
            item.setCreateTime(SubjectHelper.getAccessTime());
        }
        return result;
    }

    default SysUserAccount toSysUserAccount(SysUserGroupVo sysUserGroupVo) {
        SysUserAccount result = new SysUserAccount();
        result.setUserId(sysUserGroupVo.getId());
        if (sysUserGroupVo.getAccount() != null) {
            result.setAccountId(sysUserGroupVo.getAccount().getId());
        }
        result.setCreateTime(SubjectHelper.getAccessTime());
        result.setCreateBy(SubjectHelper.getSubjectId());
        return result;
    }
}